package org.eenie.learn.seedquartz.quartz.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.reactivex.observers.DefaultObserver;
import org.eenie.learn.seedquartz.controllers.TicketController;
import org.eenie.learn.seedquartz.quartz.manage.QuartzManager;
import org.eenie.learn.seedquartz.utils.OrderInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

public class SubmitJob implements Job {
    public static String JOB_ID = "";


    @Autowired
    OrderInfo mOrderInfo;


    @Autowired
    TicketController mTicketController;

    @Autowired
    QuartzManager mQuartzManager;

    public SubmitJob() {

    }


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        mTicketController.submit(mOrderInfo.getOrderModels())
                .subscribe(new DefaultObserver<JSONObject>() {
                    @Override
                    public void onNext(JSONObject jsonObject) {
                        if (jsonObject.getBoolean("isSuccess")) {
                            mQuartzManager.pauseTask(context.getJobDetail().getKey());
//                            mQuartzManager.resumeTask(PayNoticeJob.JOB_ID);
                            mTicketController.sendToWx("抢票成功,快去公众号付款吧", JSON.toJSONString(mOrderInfo.getOrderModels())).subscribe();

                        } else {
                            jsonObject.getString("Message");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
